package com.hd.trans;

import android.content.Context;

import com.hd.trans.config.CallbackProperty;
import com.hd.trans.config.ThemeProperty;
import com.hd.trans.config.TranslationProperty;
import com.hd.trans.ui.base.AppContextProvider;
import com.hd.trans.ui.base.TransInit;
import com.hd.trans.utils.ToastUtils;

/**
 * 翻译SDK-外部代理
 */
public class TranslationEntry {
    private static volatile TranslationEntry sInstance;
    private TranslationProperty translationProperty;
    private ThemeProperty themeProperty;
    private CallbackProperty callbackProperty;
    private boolean isInitialized = false;
    private boolean isDebug = false;


    private TranslationEntry() {
    }

    public static TranslationEntry getInstance() {
        if (sInstance == null) {
            synchronized (TranslationEntry.class) {
                if (sInstance == null) {
                    sInstance = new TranslationEntry();
                }
            }
        }
        return sInstance;
    }

    /**
     * 非向外开发函数：获取是否是调试模式
     */
    protected boolean isDebug() {
        return isDebug;
    }

    public TranslationProperty getTranslationProperty() {
        checkInitialized();
        return translationProperty;
    }

    public ThemeProperty getThemeProperty() {
        checkInitialized();
        return themeProperty;
    }

    public void setThemeProperty(ThemeProperty themeProperty) {
        this.themeProperty = themeProperty;
    }

    public CallbackProperty getCallbackProperty() {
        checkInitialized();
        return callbackProperty;
    }

    private void checkInitialized() {
        if (!isInitialized()) {
            // 抛出初始化异常
            throw new IllegalStateException("TranslationEntry has not been initialized");
        }
    }

    public void init(
            Context context,
            boolean isDebug,
            TranslationProperty translationProperty
    ) {
        init(context, isDebug, translationProperty, new ThemeProperty.Builder().build(), new CallbackProperty.Builder().build());
    }

    public void init(
            Context context,
            boolean isDebug,
            TranslationProperty translationProperty,
            ThemeProperty themeProperty
    ) {
        init(context, isDebug, translationProperty, themeProperty, new CallbackProperty.Builder().build());
    }

    public void init(
            Context context,
            boolean isDebug,
            TranslationProperty translationProperty,
            CallbackProperty callbackProperty
    ) {
        init(context, isDebug, translationProperty, new ThemeProperty.Builder().build(), callbackProperty);
    }

    /**
     * 初始化
     */
    public void init(
            Context context,
            boolean isDebug,
            TranslationProperty translationProperty,
            ThemeProperty themeProperty,
            CallbackProperty callbackProperty
    ) {
        AppContextProvider.init(context);
        ToastUtils.init(context);
        this.translationProperty = translationProperty;
        this.themeProperty = themeProperty;
        this.callbackProperty = callbackProperty;
        this.isDebug = isDebug;
        //已经初始化
        this.isInitialized = true;
        // 初始化数据
        TransInit.initialize();
    }

    /**
     * 检测是否已经初始化完成
     */
    public boolean isInitialized() {
        return isInitialized;
    }

}